package com.lovi.collect.controller;

import com.lovi.collect.service.CollectService;
import com.lovi.pojo.Collect;
import com.lovi.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *  收藏controller
 */
@RestController
@RequestMapping("collect")
@Slf4j
public class CollectController {

    @Autowired
    private CollectService collectService;

    @PostMapping("save")
    public R save(@RequestBody @Validated Collect collect, BindingResult result){
        if(result.hasErrors()){
            log.info("CollectController.save 结果为->{}", "参数错误");
            return R.fail("添加收藏参数错误",collect);
        }
        return collectService.save(collect);
    }

    @PostMapping("list")
    public R list(@RequestBody Collect collect){

        return collectService.list(collect.getUserId());
    }


    @PostMapping("remove")
    public R remove(@RequestBody Collect collect){

        return collectService.remove(collect);
    }


    @PostMapping("remove/product")
    public R removeByPid(@RequestBody Integer productId){

        return collectService.removeByPid(productId);
    }

}
